<?php
// ========================== 文件说明 ==========================//
// 本文件说明：系统设置
// --------------------------------------------------------------//
// 本程序作者：angel
// --------------------------------------------------------------//
// 本程序版本：SaBlog-X Ver 1.6
// --------------------------------------------------------------//
// 本程序主页：http://www.sablog.net
// ========================== 开发环境 ==========================//
// register_globals = Off
// --------------------------------------------------------------//
// magic_quotes_gpc = On
// --------------------------------------------------------------//
// safe_mode = On
// --------------------------------------------------------------//
// Windows server 2003 & Linux & FreeBSD
// --------------------------------------------------------------//
// Apache/1.3.33 & PHP/4.3.2 & MySQL/4.0.17
// --------------------------------------------------------------//
// Apache/1.3.34 & PHP/4.4.1 & MySQL/5.0.16
// --------------------------------------------------------------//
// Apache/2.0.55 & PHP/5.1.1 & MySQL/5.0.15
// --------------------------------------------------------------//
// Copyright (C) Security Angel Team All Rights Reserved.
// ==============================================================//

if(!defined('SABLOG_ROOT') || !isset($php_self) || !preg_match("/[\/\\\\]cp\.php$/", $php_self)) {
	exit('Access Denied');
}

//权限检查
permission();

$settingsmenu = array(
	'basic' => '常规',
	'display' => '阅读',
	'sidebar' => '侧栏',
	'comment' => '评论',
	'search' => '搜索',
	'attach' => '附件',
	'dateline' => '时间',
	'trackback' => '引用',
	'seo' => '优化',
	'wap' => 'WAP',
	'ban' => '安全',
	'rss' => '订阅',
	'permalink' => '永久链接',
);

!$type && $type = 'basic';

// 更新配置以及配置文件
if($_POST['action'] == 'updatesetting') {
	//$DB->query("TRUNCATE TABLE {$db_prefix}settings");
	foreach($_POST['setting'] as $key => $val) {
		$DB->query("REPLACE INTO {$db_prefix}settings VALUES ('".sax_addslashes($key)."', '".sax_addslashes($val)."')");
	}
	if ($type == 'sidebar') {
		links_recache();
		newcomments_recache();
		archives_recache();
		hottags_recache();
	}
	if ($type == 'permalink') {
		$permalink_type = $_POST['setting']['permalink_type'];
		if ($permalink_type != 'none') {
			$rewritecode =
				array(
					'%date%',
					'%category_name%',
					'%category_id%',
					'%article_name%',
					'%article_id%'
				);

			$rewritereplace =
				array(
					'([0-9]{6})',
					'([^/]+)',
					'([0-9]+)',
					'([^/]+)',
					'([0-9]+)'
				);

			$queryreplace =
				array (
					'',
					'curl=',
					'cid=',
					'alias=',
					'articleid='
				);

			function preg_index($number) {
				$match_prefix = '$';
				return $match_prefix.$number;
			}
			$permalink_structure = array();
			if ($permalink_type == 'archives') {
				$permalink_structure[] = '/archives/%article_id%/';
				$permalink_structure[] = '/%article_name%/';
			} elseif ($permalink_type == 'category') {
				$permalink_structure[] = '/%category_id%/%article_id%/';
				$permalink_structure[] = '/%category_id%/%article_name%/';
				$permalink_structure[] = '/%category_name%/%article_id%/';
				$permalink_structure[] = '/%category_name%/%article_name%/';
			} elseif ($permalink_type == 'date') {
				$permalink_structure[] = '/%date%/%article_id%/';
				$permalink_structure[] = '/%date%/%article_name%/';
			}

			$rules = array();
			// 只带月份的归档
			$rules['^date/([0-9]{6})/?([0-9]+)?/?$'] = 'index.php?action=article&setdate=$1&page=$2';
			// 无分类翻页
			$rules['^page/([0-9]+)?/?$'] = 'index.php?page=$1';
			// 分类
			$rules['^category/([0-9]+)/?([0-9]+)?/?$'] = 'index.php?cid=$1&page=$2';
			$rules['^category/([^/]+)/?([0-9]+)?/?$'] = 'index.php?curl=$1&page=$2';
			// 用户列表、高级搜索、注册、登陆
			$rules['^(archives|search|reg|login|index|links)/?$'] = 'index.php?action=$1';
			// 全部评论、标签列表、引用列表 带分页
			$rules['^(comments|tagslist|trackbacks|index)/?([0-9]+)?/?$'] = 'index.php?action=$1&page=$2';
			// tags
			$rules['^tag/([^/]+)/?([0-9]+)?/?$'] = 'index.php?action=tags&item=$1&page=$2';
			// 用户
			$rules['^uid/([0-9]+)/?([0-9]+)?/?$'] = 'index.php?action=finduser&uid=$1&page=$2';
			$rules['^user/([^/]+)/?([0-9]+)?/?$'] = 'index.php?action=finduser&user=$1&page=$2';
			// 地图文件
			$rules['sitemap.xml'] = 'sitemap.php';

			if ($permalink_structure) {
				foreach ($permalink_structure as $structure) {
					$mark = strstr($structure, 'article_name') ? 'alias=' : 'articleid=';
					$query_token = '^'.str_replace($rewritecode, $rewritereplace, $structure).'?([0-9]+)?/?$';
					preg_match_all('/%.+?%/', $structure, $tokens);
					$num_tokens = count($tokens[0]);
					$query_tokens = '';
					$queries = array();
					for ($i = 0; $i < $num_tokens; $i++) {
						if ($i > 0) {
							$queries[$i] = $queries[$i - 1] . '&';
						}

						$query_tokens = str_replace($rewritecode, $queryreplace, $tokens[0][$i]) . preg_index($i+1);
						$queries[$i] .= $query_tokens;
					}
					$rules[$query_token] = 'index.php?action=show&'.$queries[$num_tokens-1].'&page=$'.($i+1);
				}
			}
			$DB->query("REPLACE INTO {$db_prefix}settings VALUES ('permalink', '1')");
		}
		else {
			$DB->query("REPLACE INTO {$db_prefix}settings VALUES ('permalink', '0')");
		}
		$DB->query("REPLACE INTO {$db_prefix}settings VALUES ('permalink_rules', '".sax_addslashes(serialize($rules))."')");
	}
	settings_recache();
	redirect('更新系统配置成功', 'cp.php?job=configurate&type='.$type);
} //end update

if($_POST['action'] == 'updateping') {
	xmlrpc();
	redirect('Ping成功', 'cp.php?job=configurate&type='.$type);
}

$query = $DB->query("SELECT * FROM {$db_prefix}settings");
while($setting = $DB->fetch_array($query)) {
	$settings[$setting['title']] = htmlspecialchars($setting['value']);
}

ifchecked($settings['enable_stat'],'enable_stat');
ifchecked($settings['show_calendar'],'show_calendar');
ifchecked($settings['show_statistics'],'show_statistics');
ifchecked($settings['show_debug'],'show_debug');
ifchecked($settings['rss_all_output'],'rss_all_output');
ifchecked($settings['random_links'],'random_links');
ifchecked($settings['close_comment'],'close_comment');
ifchecked($settings['audit_comment'],'audit_comment');
ifchecked($settings['seccode'],'seccode');
ifchecked($settings['comment_order'],'comment_order');
ifchecked($settings['attachments_thumbs'],'attachments_thumbs');
ifchecked($settings['display_attach'],'display_attach');
ifchecked($settings['remote_open'],'remote_open');
ifchecked($settings['close'],'close');
ifchecked($settings['closereg'],'closereg');
ifchecked($settings['gzipcompress'],'gzipcompress');
ifchecked($settings['showmsg'],'showmsg');
ifchecked($settings['enable_trackback'],'enable_trackback');
ifchecked($settings['trackback_life'],'trackback_life');
ifchecked($settings['audit_trackback'],'audit_trackback');
ifchecked($settings['trackback_order'],'trackback_order');
ifchecked($settings['smarturl'],'smarturl');
ifchecked($settings['sitemap'],'sitemap');
ifchecked($settings['watermark'],'watermark');
ifchecked($settings['wap_enable'],'wap_enable');
ifchecked($settings['banip_enable'],'banip_enable');
ifchecked($settings['spam_enable'],'spam_enable');
ifchecked($settings['rss_enable'],'rss_enable');
ifchecked($settings['permalink'],'permalink');
ifchecked($settings['use_html'],'use_html');
ifchecked($settings['show_avatars'],'show_avatars');


$viewmode = $avatar_rating = $avatar_default = $attack_reject = $waterpos = $tb_spam_level = array();;
$viewmode[$settings['viewmode']] = 'checked';
$permalink_type[$settings['permalink_type']] = ' checked';
$attack_reject[$settings['attack_reject']] = ' checked';
$avatar_rating[$settings['avatar_rating']] = 'checked';
$avatar_default[$settings['avatar_default']] = 'checked';

$settings['server_timezone'] < 0 ? ${'zone_0'.str_replace('.','_',abs($settings['server_timezone']))}='checked' : ${'zone_'.str_replace('.','_',$settings['server_timezone'])}='selected';

$waterpos[$settings['waterpos']] = 'checked';
$tb_spam_level[$settings['tb_spam_level']] = 'checked';

$gd_version = gd_version();
$gd_version = $gd_version ? '服务器GD版本:'.$gd_version : '服务器不支持GD,因此该功能无法正常使用.';

$subnav = '';
if (in_array($type, array_flip($settingsmenu))) {
	$subnav = $settingsmenu[$type];
}

$navlink_L = ' &raquo; <a href="cp.php?job=configurate">系统设置</a>'.($subnav ? ' &raquo; '.$subnav : '');
cpheader($subnav);
include template('configurate');
?>